/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.domain;

import javax.persistence.*;
import java.util.Date;

/**
 * As the name suggests, this Class is a log of answers a user gives while
 * answering Test questions.
 * 
 * @author John Ericksen
 *
 *
 */

@Entity
@Table( name="braintrain_answerLog" )
public class AnswerLog extends BrainTrainBase implements Comparable<AnswerLog>{
    
    private static final long serialVersionUID = 5769386523254024767L;
    
    private Date submitDate;
    private boolean correct = false;
    private User user;
    
    //inverse mapping
    private Answer answer;
    private Module module;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="module_id")
    public Module getModule() {
        return module;
    }

    public void setModule(Module module) {
        this.module = module;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="answer_id")
    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }
    
    public boolean isCorrect() {
        return correct;
    }

    public void setCorrect(boolean correct) {
        this.correct = correct;
    }

    public Date getSubmitDate() {
        return submitDate == null ? null : (Date) submitDate.clone();
    }

    public void setSubmitDate(Date submitDate) {
        this.submitDate = submitDate;
    }



    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int compareTo(AnswerLog log) {
        if(log != null)
        {
            return this.getSubmitDate().compareTo(log.getSubmitDate());
        }
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        AnswerLog answerLog = (AnswerLog) o;

        if (answer != null ? !answer.equals(answerLog.answer) : answerLog.answer != null) return false;
        if (submitDate != null ? !submitDate.equals(answerLog.submitDate) : answerLog.submitDate != null) return false;
        if (user != null ? !user.equals(answerLog.user) : answerLog.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (submitDate != null ? submitDate.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        result = 31 * result + (answer != null ? answer.hashCode() : 0);
        return result;
    }
}
